﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.ICore.I_DAL;
using STHPMS1.Common.C_Entity.Stmpms1;
using System.Linq;
using System.Data;

namespace STHPMS1.BLL.B_Bus
{
    public class B_UnitBus : BllBase, IB_UnitBill
    {
        public void Save<T>(T entity)
            where T : class ,IB_Unit
        {
            this.Scene.Dal.Sthpms1.Save(entity);
        }

        public void Remove(IB_Unit entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IB_Unit
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }


        public double GetLenght(string unit)
        {
            B_Unit info = Get<B_Unit>(q => q.Unit == unit, null).ToList()[0];
            return info.Endmp - info.Beginmp;
        }

        public DataTable GetUnitZB(string ldID)
        {
            return this.Scene.Dal.Sthpms1.B_Unit.GetUnitZB(ldID);
        }

        public DataTable GetUnitZBByRdid(string rdid,int dir)
        {
            return this.Scene.Dal.Sthpms1.B_Unit.GetUnitZBByRdid(rdid,dir);
        }

        /// <summary>
        /// 根据路线和方向查找指定单元
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dir"></param>
        /// <param name="rdid"></param>
        /// <returns></returns>
        public List<T> GetUnitList<T>(string dir, string rdid) where T : class,IB_Unit
        {
            return this.Scene.Dal.Sthpms1.B_Unit.GetUnitList<T>(dir, rdid);
        }


        /*
        /// <summary>
        /// 返回单元
        /// </summary>
        public string GetUnit(double start,double end,string dir,string rdid)
        {
            //得到路线下的所有指定方向的路段


            //遍历路段下的所有单元,模糊匹配

            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            List<B_Unit> lst = dao.Select<B_Unit>(x => x.Beginmp == start && x.Endmp == end);
            if (lst == null)
            {
                return null;
            }
            else if (lst.Count == 0)
            {
                return null;
            }
            else
            {
                foreach (B_Unit unit in lst)
                {
                    if (unit.Unit.IndexOf(dir) > -1 && unit.Unit.IndexOf(rdid) > -1)
                    {
                        return unit.Unit;//返回单元
                    }
                }
                return null;
            }
            
        }
        */


        //public double GetMP(string inspecid, string rdwayid, int d)
        //{
        //    double minvalue = GetMinBeginMP(inspecid, rdwayid, d);
        //    double maxvalue = GetMaxEndMP(inspecid, rdwayid, d);
        //    return maxvalue - minvalue;

        //}

        //public double GetMinBeginMP(string inspecid, string rdwayid, int d)
        //{
        //    return this.Scene.Dal.Sthpms1.B_Unit.GetMinBeginMP(inspecid, rdwayid, d);
        //}

        //public double GetMaxEndMP(string inspecid, string rdwayid, int d)
        //{
        //    return this.Scene.Dal.Sthpms1.B_Unit.GetMinBeginMP(inspecid, rdwayid, d);
        //}

        public List<string> List(string rdwayid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Column<string>(dao.From<B_Unit>().Select(x => x.Unit)
                         .Where(q => q.Unit.StartsWith("DY_" + rdwayid)));
        }

        public T GetSingle<T>(string unit) where T : class, IB_Unit
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Single<T>(x => x.Unit == unit);
        }


        public DataRow GetUnitAvgZB(string segId)
        {
            return this.Scene.Dal.Sthpms1.B_Unit.GetUnitAvgZB(segId);
        }

        public List<string> GetUnits(string segId)
        {
            return this.Scene.Dal.Sthpms1.B_Unit.GetUnits(segId);
        }
    }
}
